<?php
/*******************************************************************************
 * Project: Bridge for WordPress to phpBB 3.0.x
 * File:    widget-links.php
 * Author:  The Greek WordPress Comunity
 * Date:    Tuesday, 1st February 2011
 * -----------------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
 
class phpBB3Links extends WP_Widget
{
    function phpBB3Links()
    {
        /* Widget settings. */
        $widget_ops = array(
            'classname' => 'phpBB3 Links Widget',
            'description' => __('Allows you to display a list of links to your phpBB.', 'wp_phpbb_bridge')
        );
        
        /* Widget control settings. */
        $control_ops = array(
            'width' => 250,
            'height' => 250,
            'id_base' => 'phpbb3-links-widget'
        );
        
        /* Create the widget. */
        $this->WP_Widget(
            'phpbb3-links-widget', 
            'phpBB3 Links Widget', 
            $widget_ops, 
            $control_ops
        );
    }
    
    function form($instance)
    {
        $defaults = array(
                        'title'=>'Forum Links',
        				'display_index'=>'yes',
        				'display_faq'=>'yes',
        				'display_search'=>'yes',
        				'display_active_topics'=>'yes',
        				'display_unanswered_posts'=>'yes',
        				'display_your_posts'=>'yes',
        				'display_new_posts'=>'yes',
        				'display_pms'=>'yes',
        				'display_send_pm'=>'yes',
        				'display_members'=>'yes',
        				'display_team'=>'yes',
        				'display_whos_online'=>'yes',
                    );
                    
        $instance = wp_parse_args(
                        (array)$instance, 
                        $defaults
                    );
    ?>
    <div class="widget-content">
		<p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php 
                    echo _e('Title:', 'wp_phpbb_bridge'); 
                ?>
            </label>
            <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
        </p>
		<p>
            <input name="<?php echo $this->get_field_name('display_index'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_index'); ?>" value="yes" <?php if($instance['display_index'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_index'); ?>">
                <?php 
                    echo _e('Display Forum Index Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p>
            <input name="<?php echo $this->get_field_name('display_faq'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_faq'); ?>" value="yes" <?php if($instance['display_faq'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_faq'); ?>">
                <?php 
                    echo _e('Display FAQ Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p>
            <input name="<?php echo $this->get_field_name('display_search'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_search'); ?>" value="yes" <?php if($instance['display_search'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_search'); ?>">
                <?php 
                    echo _e('Display Forum Search Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_active_topics'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_active_topics'); ?>" value="yes" <?php if($instance['display_active_topics'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_active_topics'); ?>">
                <?php 
                    echo _e('Display View Active Topics Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_unanswered_posts'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_unanswered_posts'); ?>" value="yes" <?php if($instance['display_unanswered_posts'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_unanswered_posts'); ?>">
                <?php 
                    echo _e('Display View Unanswered Posts Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_your_posts'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_your_posts'); ?>" value="yes" <?php if($instance['display_your_posts'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_your_posts'); ?>">
                <?php 
                    echo _e('Display View Your Posts Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_new_posts'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_new_posts'); ?>" value="yes" <?php if($instance['display_new_posts'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_new_posts'); ?>">
                <?php 
                    echo _e('Display View New Posts Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_pms'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_pms'); ?>" value="yes" <?php if($instance['display_pms'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_pms'); ?>">
                <?php 
                    echo _e('Display Private Messages Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_send_pm'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_send_pm'); ?>" value="yes" <?php if($instance['display_send_pm'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_send_pm'); ?>">
                <?php 
                    echo _e('Display Send Private Message Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_members'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_members'); ?>" value="yes" <?php if($instance['display_members'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_members'); ?>">
                <?php 
                    echo _e('Display Member List Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_team'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_team'); ?>" value="yes" <?php if($instance['display_team'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_team'); ?>">
                <?php 
                    echo _e('Display The Team Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('display_whos_online'); ?>" type="checkbox" id="<?php echo $this->get_field_id('display_whos_online'); ?>" value="yes" <?php if($instance['display_whos_online'] == 'yes'){ echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('display_whos_online'); ?>">
                <?php 
                    echo _e('Display Who is Online Link:', 'wp_phpbb_bridge'); 
                ?>
            </label>
        </p>
    </div>
        <?php
    }
    
    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        
        $instance['title'] = $new_instance['title'];
        $instance['display_index'] = $new_instance['display_index'];
        $instance['display_faq'] = $new_instance['display_faq'];
        $instance['display_search'] = $new_instance['display_search'];
        $instance['display_active_topics'] = $new_instance['display_active_topics'];
        $instance['display_unanswered_posts'] = $new_instance['display_unanswered_posts'];
        $instance['display_your_posts'] = $new_instance['display_your_posts'];
        $instance['display_new_posts'] = $new_instance['display_new_posts'];
        $instance['display_pms'] = $new_instance['display_pms'];
        $instance['display_send_pm'] = $new_instance['display_send_pm'];
        $instance['display_members'] = $new_instance['display_members'];
        $instance['display_team'] = $new_instance['display_team'];
        $instance['display_whos_online'] = $new_instance['display_whos_online'];
        
        return $instance;
    }
    
    function widget($args, $instance)
    {
        extract($args);

        $ucp_url = trim(get_option('wpb_url'));
        $forum_url = str_replace("/ucp.php", "/", $ucp_url);
        $search_url = $forum_url . 'search.php';
        $member_url = $forum_url . 'memberlist.php';
        $session_id = 'sid=' . wpb_get_sessionid();
        
        $title = $instance['title'];
        $display_index = $instance['display_index'];
        $display_faq = $instance['display_faq'];
        $display_search = $instance['display_search'];
        $display_active_topics = $instance['display_active_topics'];
        $display_unanswered_posts = $instance['display_unanswered_posts'];
        $display_your_posts = $instance['display_your_posts'];
        $display_new_posts = $instance['display_new_posts'];
        $display_pms = $instance['display_pms'];
        $display_send_pm = $instance['display_send_pm'];
        $display_members = $instance['display_members'];
        $display_team = $instance['display_team'];
        $display_whos_online = $instance['display_whos_online'];
        
        echo $before_widget . $before_title . $title . $after_title . '<ul>';
        
        if($display_index == 'yes')
        {
            echo '<li><a href="' . $forum_url . '?' . $session_id . '">';
            echo _e('Forum index', 'wp_phpbb_bridge');
            echo '</a></li>';
        }
        
        if($display_faq == 'yes')
        {
            echo '<li><a href="' . $forum_url . 'faq.php?' . $session_id . '">';
            echo _e('FAQ', 'wp_phpbb_bridge');
            echo '</a></li>';
        }
        
        if($display_search == 'yes')
        {
            echo '<li><a href="' . $search_url . '?' . $session_id . '">';
            echo _e('Forum search', 'wp_phpbb_bridge');
            echo '</a></li>';
        }
        
        if($display_active_topics == 'yes')
        {
            echo '<li><a href="' . $search_url . '?search_id=active_topics&amp;' . $session_id . '">';
            echo _e('View active topics', 'wp_phpbb_bridge');
            echo '</a></li>';
        }
        
        if($display_unanswered_posts == 'yes')
        {
            echo '<li><a href="' . $search_url . '?search_id=unanswered&amp;' . $session_id . '">';
            echo _e('View unanswered posts', 'wp_phpbb_bridge');
            echo '</a></li>';
        }
        
        if(wpb_is_user_logged_in())
        {
            if($display_your_posts == 'yes')
            {
                echo '<li><a href="' . $search_url . '?search_id=egosearch&amp;' . $session_id . '">';
                echo _e('View your posts', 'wp_phpbb_bridge');
                echo '</a></li>';
            }
            
            if($display_new_posts == 'yes')
            {
                echo '<li><a href="' . $search_url . '?search_id=newposts&amp;' . $session_id . '">';
                echo _e('View new posts', 'wp_phpbb_bridge');
                echo '</a></li>';
            }
            
            if($display_pms == 'yes')
            {
                echo '<li><a href="' . $ucp_url . '?i=pm&amp;folder=inbox&amp;' . $session_id . '">';
                echo _e('Private messages', 'wp_phpbb_bridge');
                echo '</a></li>';
            }
            
            if($display_send_pm == 'yes')
            {
                echo '<li><a href="' . $ucp_url . '?i=pm&amp;mode=compose&amp;' . $session_id . '">';
                echo _e('Send private message', 'wp_phpbb_bridge');
                echo '</a></li>';
            }
            
            if($display_members == 'yes')
            {
                echo '<li><a href="' . $member_url . '?' . $session_id . '">';
                echo _e('Member list', 'wp_phpbb_bridge');
                echo '</a></li>';
            }
            
            if($display_team == 'yes')
            {
                echo '<li><a href="' . $member_url . '?mode=leaders&amp;' . $session_id . '">';
                echo _e('The team', 'wp_phpbb_bridge');
                echo '</a></li>';
            }
            
            if($display_whos_online == 'yes')
            {
                echo '<li><a href="' . $forum_url . 'viewonline.php?' . $session_id . '">';
                echo _e('Who is online', 'wp_phpbb_bridge');
                echo '</a></li>';
            }
        }
        
        echo '</ul>' . $after_widget;
    }
    
}

function registerLinksWidget()
{
    register_widget('phpBB3Links');
}

add_action('widgets_init', 'registerLinksWidget');

?>